<?php

namespace app\home\controller;

use app\home\model\Login as LoginModel;
use think\Controller;
use think\Session;
use think\captcha\Captcha;
use think\Request;

class Login extends Controller
{
	public function index()
	{
		return $this->fetch('index');
	}

	public function login(Request $request)
	{	
		// 接收数据
		$post = $request->post();
		// 判断验证码
		$captcha = new Captcha();
		if( !$captcha->check($post['code']))
		{
			$this->error('验证码错误','login/index');die;
		}
		// 接收表单数据
		$tel = $post['tel'];
		$pwd = MD5($post['pwd']);

		// 验证表单数据
		$res = LoginModel::field('uid,userName')
				->where(array('tel'=>$tel,'password'=>$pwd))
				->find();
    	if ( !$res ) {
    		$this->error('登录失败','login/index');die;
    	}
    	// 储存session
    	Session::set('uid',$res['uid']);
    	Session::set('userName',$res['userName']);
    	$this->success('登录成功','common/index');die;
	}

	public function register()
	{
		return $this->fetch('register');
	}

	// 退出登陆
	public function loginOut()
	{
		session(null);
		if ( empty(Session::get()) ) {
			$this->success('安全退出','index/index');die;
		}
	}

}
